﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Reflection;

namespace SERP.Commons
{
    public class Common
    {
        public static void AddCheckedToTable(DataTable tblData)
        {
            AddCheckedToTable(tblData, true);
        }
        public static void AddCheckedToTable(DataTable tblData, bool Value)
        {
            DataColumn col = new DataColumn("CHECK", typeof(Boolean));
            tblData.Columns.Add(col);
            ResetTableChecked(tblData, Value);
        }
        public static void AddIndexToTable(DataTable tblData)
        {
            DataColumn col = new DataColumn("STT", typeof(Int32));
            if (!tblData.Columns.Contains("STT"))
            {
                tblData.Columns.Add(col);
            }
            ResetTableIndex(tblData);
        }
        public static void ResetTableChecked(DataTable tblData, bool Value)
        {
            for (int i = 0; i < tblData.Rows.Count; i++)
            {
                tblData.Rows[i]["CHECK"] = Value;
            }
        }
        public static void ResetTableIndex(DataTable tblData)
        {
            for (int i = 0; i < tblData.Rows.Count; i++)
            {
                tblData.Rows[i]["STT"] = i + 1;
            }
        }
        public static DataTable FilterTable(DataTable dt, string filterString)
        {
            DataRow[] filteredRows = dt.Select(filterString);
            DataTable filteredDt = dt.Clone();

            DataRow dr;
            foreach (DataRow oldDr in filteredRows)
            {
                dr = filteredDt.NewRow();
                for (int i = 0; i < dt.Columns.Count; i++)
                    dr[dt.Columns[i].ColumnName] = oldDr[dt.Columns[i].ColumnName];
                filteredDt.Rows.Add(dr);
            }
            return filteredDt;
        }
        public static DataTable UnionTable(DataTable First, DataTable Second)
        {
            //Result table
            DataTable table = new DataTable("Union");

            //Build new columns
            DataColumn[] newcolumns = new DataColumn[First.Columns.Count];

            for (int i = 0; i < First.Columns.Count; i++)
            {
                newcolumns[i] = new DataColumn(First.Columns[i].ColumnName, First.Columns[i].DataType);
            }

            //add new columns to result table
            table.Columns.AddRange(newcolumns);

            table.BeginLoadData();

            //Load data from first table
            foreach (DataRow row in First.Rows)
            {
                table.LoadDataRow(row.ItemArray, true);
            }

            //Load data from second table
            foreach (DataRow row in Second.Rows)
            {
                table.LoadDataRow(row.ItemArray, true);
            }

            table.EndLoadData();

            return table;


        }
        //Convert khong dau
        public static string Convert_ASC(string strInput)
        {
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = strInput.Normalize(NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }
        public static Decimal ConvertNumber(object obj)
        {
            decimal result = 0;
            string str = obj.ToString();
            decimal.TryParse(str, out  result);
            return result;
        }

        public static bool IsNumber(object obj)
        {
            decimal rval = 0;
            return decimal.TryParse(obj.ToString(), out rval);
        }
        public static string SplitSo(string item)
        {
            if (item == "") return "0";
            int t = 0;
            string k = "";
            string so = "";
            bool re = false;
            int y = 0;
            for (int i = 0; i < item.Length; i++)
            {
                y = 0;
                k = item.Substring(i, 1);
                t = (int)Convert.ToChar(k);
                for (int x = 48; x <= 57; x++)
                {
                    if (t == x)
                        so += k;
                    else
                    {
                        re = true;
                        y++;
                    }
                }
                if ((re) && (y == 10))
                    break;
            }
            if (so == "")
                so = "0";
            return so;
        }

        public static DateTime BeginDay(DateTime date)
        {
            DateTime BeforDate = date.AddDays(-1);
            return new DateTime(BeforDate.Year, BeforDate.Month, BeforDate.Day, 23, 59, 59);
        }
        public static DateTime EndDay(DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);
        }

        public static DataTable ReadExcellFile(string FileName, string SheetName)
        {
            string strConn = "";
            if (System.IO.Path.GetExtension(FileName).ToLower() == ".xlsx")
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
            }
            else
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=Excel 8.0;";
            }
            System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" + SheetName + "$]", strConn);
            DataSet myDataSet = new DataSet();
            try
            {
                myCommand.Fill(myDataSet, "TableName");
            }
            catch
            {
                myDataSet.Tables.Add(new DataTable());
            }
            return myDataSet.Tables[0];
        }

    }


}
